package com.laxcen.facestaticcheck.engine;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.Nullable;

import com.laxcen.facestaticcheck.Const;

/**
 * 人脸识别管理
 */
public class FaceCheckManager {
    private static FaceCheckManager faceCheckManager;
    public static final int FACE_REGISTER = 1012;//人脸注册
    public static final int FACE_COMPARE= 1013;//人脸对比
    public static final String FACE_CONFIDENCE = "FACE_CONFIDENCE";//人脸识别度
    public static final String FACE_BASE64 = "FACE_BASE64";//人脸数据
    public static FaceCheckManager getInstance(){
        if(faceCheckManager==null){
            synchronized (FaceCheckManager.class){
                if(faceCheckManager==null){
                    faceCheckManager = new FaceCheckManager();
                }
            }
        }
        return faceCheckManager;
    }

    /**
     * 关闭人脸识别界面
     * @param context
     */
    public void closeFaceCheck(Context context){
        Intent intent = new Intent(context,FaceCheckActivity.class);
        intent.putExtra("close",true);
        context.startActivity(intent);
    }

    /**
     * 人脸注册
     */
    public void startRegister(Activity activity, OnCallback onCallback){
        this.registerCallback = onCallback;
        Intent intent = new Intent(activity,FaceCheckActivity.class);
        intent.putExtra("type",FACE_REGISTER);
        activity.startActivityForResult(intent,FACE_REGISTER);
    }

    /**
     * 人脸登录
     * @param face64
     * @param onCallback
     */
    public void startCompare(Activity activity,String face64,OnCallback onCallback){
        this.compareCallback = onCallback;
        Intent intent = new Intent(activity,FaceCheckActivity.class);
        intent.putExtra("type",FACE_COMPARE);
        intent.putExtra("face64",face64);
        activity.startActivityForResult(intent,FACE_COMPARE);
    }

    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(resultCode==Activity.RESULT_OK){
            switch (requestCode){
                case FACE_REGISTER:
                    String face64 = data.getStringExtra(FACE_BASE64);
                    if(registerCallback!=null){
                        registerCallback.onResult(true,face64);
                    }else{
                        registerCallback.onResult(false,"获取人脸数据失败");
                    }
                    break;
                case FACE_COMPARE:
                    double faceConfidence = data.getDoubleExtra(FACE_CONFIDENCE,0);
                    Log.e("compareCallback","人脸识别度:"+faceConfidence);
                    if(compareCallback!=null) {
                        if (faceConfidence >= Const.FACE_CONFIDENCE) {
                            compareCallback.onResult(true,"识别成功!");
                        } else {
                            compareCallback.onResult(false,"识别失败!");
                        }
                    }
                    break;
            }
        }
    }

    private OnCallback registerCallback,compareCallback;
    /**
     * 回调
     */
    public interface OnCallback{
        void onResult(boolean isSuccess,String msg);
    }
}
